package com.testtask.rightandabove.tools;

import com.testtask.rightandabove.entity.CD;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
public class Pager {

    private static Integer lastPage;
    private Integer entitiesPerPage = 10;

    public List<CD> findAllPage(List<CD> cdList, String pageStr) {
        List<CD> subList = new ArrayList<CD>();
        Integer quantityItems = cdList.size();
        if (quantityItems != 0) {
            Integer difference = 0;
            if (quantityItems % entitiesPerPage == 0) {
                lastPage = quantityItems / entitiesPerPage;
            } else {
                lastPage = quantityItems / entitiesPerPage + 1;
                difference = lastPage * entitiesPerPage - quantityItems;
            }
            Integer page = Integer.parseInt(pageStr);
            if ((page + 1) != lastPage) {
                subList = cdList.subList(entitiesPerPage * page, (entitiesPerPage * page) + entitiesPerPage);
            } else {
                subList = cdList.subList(entitiesPerPage * page, (entitiesPerPage * page) + entitiesPerPage - difference);
            }
        }
        return subList;
    }

    public Integer getEntitiesPerPage() {
        return entitiesPerPage;
    }

    public void setEntitiesPerPage(Integer entitiesPerPage) {
        this.entitiesPerPage = entitiesPerPage;
    }

    public static Integer getLastPage() {
        return lastPage;
    }

    public static void setLastPage(Integer lastPage) {
        Pager.lastPage = lastPage;
    }
}
